ccR.Death_Knight = {
	Blood_Boil =
	function(count, runeType)
		if ((ccR.Player.Has.Glyph(GLYPH_OF_BLOOD_BOIL) and ccR.Player.Count(15) >= count) or (not ccR.Player.Has.Glyph(GLYPH_OF_BLOOD_BOIL) and ccR.Player.Count(10) >= count))	then
			-- Only use a blood rune
			if runeType == "onlyBlood" then
				local AruneType = GetRuneType(1) local BruneType = GetRuneType(2) 
				local _, _, AruneReady = GetRuneCooldown(1) local _, _, BruneReady = GetRuneCooldown(2)
				if ((AruneType == 1 and AruneReady) or (BruneType == 1 and BruneReady))	then ccR.Cast.Spell(BLOOD_BOIL) end
			end
			-- Only a blood or blood converted to death rune
			if runeType == "onlyBloodorDeath" then
				local _, _, AruneReady = GetRuneCooldown(1) local _, _, BruneReady = GetRuneCooldown(2)
				if (AruneReady or BruneReady) then ccR.Cast.Spell(BLOOD_BOIL) end
			end
			-- Anything available
			if (runeType == "anyRune" or runeType == nil) then ccR.Cast.Spell(BLOOD_BOIL) end
		end
	end,
	Blood_Boil_On_Proc = function(count) if ccR.Spell.Is.Ready(BLOOD_BOIL) and ccR.Player.Has.Buff(CRIMSON_SCOURGE) and ((ccR.Player.Has.Glyph(GLYPH_OF_BLOOD_BOIL) and ccR.Player.Count(15) >= count) or (not ccR.Player.Has.Glyph(GLYPH_OF_BLOOD_BOIL) and ccR.Player.Count(10) >= count)) then ccR.Cast.Spell(BLOOD_BOIL) end end,
	Blood_Boil_Spread = function(count)
		if count == nil then count = 2 end
		local vBloodBoilDistance = 10
		if ccR.Player.Has.Glyph(GLYPH_OF_BLOOD_BOIL) then vBloodBoilDistance = 15 end
		if ccR.Spell.Is.Ready(BLOOD_BOIL)
		and ccR.Player.Count(vBloodBoilDistance) >= count
		then
			local vMissingDiseases = 0
			-- Find how many are missing blood plague
			for x = 1, # ccR_Enemies_Within_16 do
				local vUnit = ccR_Enemies_Within_16[x]
				if (
						(not ccR.Player.Has.Talent(NECROTIC_PLAGUE) and not ccR.Unit.Has.MyDebuff(vUnit, BLOOD_PLAGUE)) 
						or
						(ccR.Player.Has.Talent(NECROTIC_PLAGUE) and not ccR.Unit.Has.MyDebuff(vUnit, NECROTIC_PLAGUE_DEBUFF))
					)
				and ccR.Unit.Distance(vUnit, "player") <= vBloodBoilDistance
				then
					vMissingDiseases = vMissingDiseases + 1 
				end
			end
			if vMissingDiseases > 0 then
				-- Apply Outbreak/Plague Strike to at least one
				if ccR.Spell.Is.Ready(OUTBREAK) 
				or ccR.Spell.Is.Ready(PLAGUE_STRIKE)
				then
					for x = 1, # ccR_Enemies_Within_16 do
						local vUnit = ccR_Enemies_Within_16[x]
						if 
						(
							(not ccR.Player.Has.Talent(NECROTIC_PLAGUE) and not ccR.Unit.Has.MyDebuff(vUnit, BLOOD_PLAGUE)) 
							or
							(ccR.Player.Has.Talent(NECROTIC_PLAGUE) and not ccR.Unit.Has.MyDebuff(vUnit, NECROTIC_PLAGUE_DEBUFF))
						)
						and ccR.Player.Is.Facing(vUnit)
						and ccR.Unit.Distance(vUnit, "player") <= vBloodBoilDistance
						and (ccR.Spell.CanAttack(OUTBREAK, vUnit) or ccR.Spell.CanAttack(PLAGUE_STRIKE, vUnit))
						then
							if ccR.Spell.CanAttack(OUTBREAK, vUnit) then ccR.Cast.Attack(OUTBREAK, vUnit) end
							if ccR.Spell.CanAttack(PLAGUE_STRIKE, vUnit) then ccR.Cast.Attack(PLAGUE_STRIKE, vUnit) end
						end
					end
				end
			end
			-- Find if one has both
			local vHasDiseases = false
			for x = 1, # ccR_Enemies_Within_16 do
				local vUnit = ccR_Enemies_Within_16[x]
				if ((ccR.Unit.Has.MyDebuff(vUnit, BLOOD_PLAGUE) and ccR.Unit.Has.MyDebuff(vUnit, FROST_FEVER)) or ccR.Unit.Has.MyDebuff(vUnit, NECROTIC_PLAGUE_DEBUFF))
				and ccR.Unit.Distance(vUnit, "player") <= vBloodBoilDistance
				then
					vHasDiseases = true
				end
			end
			-- If any's missing and one does have both, Blood Boil to spread
			if vMissingDiseases > 0	and vHasDiseases then ccR.Cast.Spell(BLOOD_BOIL) return end
		end		
	end,
	Death_And_Decay = 
	function(count)
		local vDnD = DEATH_AND_DECAY
		if ccR.Player.Has.Talent(DEFILE) then vDnD = DEFILE end
		if ccR.Spell.Is.Ready(vDnD)
		and ccR.Target.Count(10) >= count 
		and ccR.Target.Distance() <= 30 
		then 
			ccR.Cast.AoE(vDnD, "target", 30, 10) 
		end
	end,
	Death_And_Decay_On_Proc =
	function(count)
		local vDnD = DEATH_AND_DECAY
		if ccR.Player.Has.Talent(DEFILE) then vDnD = DEFILE end
		if ccR.Spell.Is.Ready(vDnD) 
		and ccR.Target.Count(10) >= count 
		and ccR.Player.Has.Buff(CRIMSON_SCOURGE) 
		and ccR.Target.Distance() <= 30 
		then
			ccR.Cast.AoE(vDnD, "target", 30, 10) 
		end
	end,
	Dark_Succor = function(health) if health == nil then health = 90 end if ccR.Spell.CanAttack(DEATH_STRIKE) and ccR.Player.HasBuff(DARK_SUCCOR) and ccR.Player.Health() <= health and ccR.Player.Is.Facing("target") then ccR.Cast.Attack(DEATH_STRIKE) end end,
	Empower_Rune_Weapon = function() if ccR.Spell.Is.Ready(EMPOWER_RUNE_WEAPON) then local vRuneOnCD = 0 for i=1,6 do local start, duration, runeReady = GetRuneCooldown(i) if runeReady == false then vRuneOnCD = vRuneOnCD + 1 end end	if vRuneOnCD >= 6 and ccR.Player.Runic_Power() <= 40 then ccR.Cast.Spell(EMPOWER_RUNE_WEAPON) end end end,
	Mind_Freeze = 
	function(time) 
		if time == nil then time = 1 end
		if ccR.Spell.Is.Ready(MIND_FREEZE) then
			for x = 1, # ccR_Enemies_Within_8 do
				local vUnit = ccR_Enemies_Within_8[x]
				if ccR.Unit.Exists(vUnit)
				and ccR.Unit.Is.CastingOrChanneling(vUnit) 
				and ccR.Unit.Is.Interuptable(vUnit) 
				and ccR.Player.Is.Facing(vUnit)
				and ccR.Spell.CanAttack(MIND_FREEZE, vUnit)
				then 
					if ccR.Unit.Has.LeftOnCast(vUnit, time) then ccR.Cast.Attack(MIND_FREEZE, vUnit) end 
					if ccR.Unit.Is.Channeling(vUnit) then ccR.Cast.Attack(MIND_FREEZE, vUnit) end 
				end
			end
		end
	end,
	Strangulate = 
	function(time)
		if time == nil then time = 1 end
		if ccR.Spell.Is.Ready(STRANGULATE) then
			for x = 1, # ccR_Enemies_Within_44 do
				local vUnit = ccR_Enemies_Within_44[x]
				if ccR.Unit.Exists(vUnit)			
				and ccR.Unit.Is.CastingOrChanneling(vUnit) 
				and ccR.Unit.Is.Interuptable(vUnit) 
				and ccR.Player.Is.Facing(vUnit) 
				and not ccR.Player.Has.Talent(ASPHYXIATE) 
				and (not ccR.Spell.CanAttack(MIND_FREEZE, vUnit) or not ccR.Spell.Is.Ready(MIND_FREEZE)) 
				then 
					if ccR.Unit.Has.LeftOnCast(vUnit, time) then ccR.Cast.Attack(STRANGULATE, vUnit) end
					if ccR.Unit.Is.Channeling(vUnit) then ccR.Cast.Attack(STRANGULATE, vUnit) end 
				end 
			end
		end
	end,
	Icebound_Fortitude = 
	function(health)
		if health == nil then health = 40 end
		if ccR.Spell.Is.Ready(ICEBOUND_FORTITUDE) 
		and ccR.Player.Health() <= health 
		then 
			ccR.Cast.Spell(ICEBOUND_FORTITUDE) 
		end 
	end,
	Anti_Magic_Shell =
	function()
		if ccR.Spell.Is.Ready(ANTI_MAGIC_SHELL) then
			for x = 1, # ccR_Enemies_Within_44 do
				local vUnit = ccR_Enemies_Within_44[x]
				if ccR.Unit.Exists(vUnit)
				and (ccR.Unit.Is.CastingAtPlayer(vUnit) or ccR.Unit.Is.ChannelingAtPlayer(vUnit))
				and (
						(not ccR.Spell.CanAttack(MIND_FREEZE, vUnit) and not ccR.Spell.CanAttack(STRANGULATE, vUnit))
						or
						not ccR.Unit.Is.Interuptable(vUnit)
					)
				then
					ccR.Cast.Spell(ANTI_MAGIC_SHELL)
				end
			end
		end
	end,
	Lichborne_Healing = function(health, runicPower) if ccR.Spell.Is.Ready(LICHBORNE) and ccR.Player.Runic_Power() > runicPower and ccR.Player.Health() <= health then ccR.Cast(LICHBORNE) end if ccR.Player.Has.Buff(LICHBORNE) and ccR.Spell.CanAttack(DEATH_COIL, "player") then ccR.Cast.Spell(DEATH_COIL, "player") end end,
	Death_Grip =
	function(name)
		if name ~= nil then
			if ccR.Player.Enemies() > 0 then
				for x = 1, # ccR_Enemies_In_Combat do
					local vUnit = ccR_Enemies_In_Combat[x]
					if ccR.Unit.Is.Attackable(vUnit, "player")
					and ccR.Player.Is.Facing(vUnit)
					and ccR.Unit.Name(vUnit) == name
					and ccR.Spell.CanAttack(DEATH_GRIP, vUnit)
					and ccR.Unit.Distance(vUnit, "player") >= 10
					then
						ccR.Cast.Attack(DEATH_GRIP, vUnit)
					end
				end
			end			
		else
			if ccR.Spell.CanAttack(DEATH_GRIP) and ccR.Target.Distance() >= 10 then	ccR.Cast.Attack(DEATH_GRIP) end
		end
	end,
}
setmetatable(ccR.Death_Knight, ccR)

ccR.Death_Knight.Features = {
	Soul_Reaper = 
	function(health, runeOnly)
		local vSoulReaper
		local vRuneType
		local AruneReady
		local BruneReady
		if ccR.Player.Specialization() == 1 then vSoulReaper = BLOOD_SOUL_REAPER vRuneType = 1 end
		if ccR.Player.Specialization() == 2 then vSoulReaper = FROST_SOUL_REAPER vRuneType = 2 end
		if ccR.Player.Specialization() == 3 then vSoulReaper = UNHOLY_SOUL_REAPER vRuneType = 3 end
		-- Only use the DK's Spec main rune
		if runeOnly ~= nil then
			if vRuneType == 1 then _, _, AruneReady = GetRuneCooldown(1) _, _, BruneReady = GetRuneCooldown(2) end
			if vRuneType == 2 then _, _, AruneReady = GetRuneCooldown(5) _, _, BruneReady = GetRuneCooldown(6) end
			if vRuneType == 3 then _, _, AruneReady = GetRuneCooldown(3) _, _, BruneReady = GetRuneCooldown(4) end
		end
		if ccR.Spell.Is.Ready(vSoulReaper) then
			for x = 1, # ccR_Enemies_Within_12 do
				local vUnit = ccR_Enemies_Within_12[x]
				if ccR.Unit.Exists(vUnit)
				and ccR.Unit.Is.Alive(vUnit)
				and ccR.Unit.Health(vUnit) <= health
				and ccR.Player.Is.Facing(vUnit)
				and ccR.Spell.CanAttack(vSoulReaper, vUnit)
				then
					if runeOnly ~= nil then
						if (AruneReady or BruneReady) then ccR.Cast.Attack(vSoulReaper, vUnit) end
					else
						ccR.Cast.Attack(vSoulReaper, vUnit)
					end
				end
			end
		end
	end,
}
setmetatable(ccR.Death_Knight.Features, ccR.Death_Knight)